package com.jiuzhi.app.common.util;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by tdk on 17/9/18.
 */
public class R<T> extends HashMap<String, Object> {

    public static final Integer OK = 0;
    public static final Integer ERROR = 500;

    private Integer code;
    private String message;
    private String url;
    private T data;

    public static <T> R<T> newInstance() {
        return new R<>();
    }


    /**
     * @param c
     * @param data
     * @param message
     * @param <T>
     * @return
     */
    public static <T> R<T> genResultInfo(Integer c,T data,String message){
        R<T> result = R.newInstance();
        result.setCode(c);
        result.setData(data);
        result.setMessage(message);
        return result;
    }

    public R() {
        put("code", 0);
    }

    public static R error() {
        return error(500, "未知异常，请联系管理员");
    }

    public static R error(String msg) {
        return error(500, msg);
    }

    public static R error(int code, String msg) {
        R r = new R();
        r.put("code", code);
        r.put("msg", msg);
        return r;
    }

    public static R ok(String msg) {
        R r = new R();
        r.put("msg", msg);
        return r;
    }

    public static R ok(Map<String, Object> map) {
        R r = new R();
        r.putAll(map);
        return r;
    }

    public static R ok() {
        return new R();
    }

    public R put(String key, Object value) {
        super.put(key, value);
        return this;
    }

    /**
     * 成功
     * @param data
     * @param <T>
     * @return
     */
    public static <T> R<T> genOkResultInfo(T data){
        return genResultInfo(OK,data,null);
    }


    public static Integer getIsOK() {
        return OK;
    }

    public static Integer getERROR() {
        return ERROR;
    }

    public boolean isOk(){
        return code == OK;
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }
}
